﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Express.H.Presenter;
using KZ.Express.H.ENT;
using KZ.Utilities;
using System.Xml;
using System.Xml.Linq;
using System.Resources;
using KZ.Express.H.Properties;

namespace KZ.Express.H.Orders
{
    public partial class OrderLogDetailView : BaseView
    {
        #region Fields

        ResourceManager rm = new ResourceManager("KZ.Express.H.Properties.Resources", typeof(Resources).Assembly);
        string[] excludedProp = { 
                                    "CreatedBy", "CreatedDate", "UpdatedBy", "UpdatedDate", "IsDeleted", "PriceLists_IsOriginal",
                                    "PriceListDetails_Type", "PriceListDetails_Zone", "PriceListDetails_ListPrice", "PriceListDetails_FromWeight", "PriceListDetails_ToWeight",
                                    "Carriers_OrderNumberLength"
                                };

        #endregion

        #region Private Methods

        private void OrderLogDetailView_Load(object sender, EventArgs e)
        {
            _presenter.LoadEntity<LogENT>();
        }

        private StringBuilder ShowUpdateLog(LogENT log, XmlDocument xmlNewDoc, XmlDocument xmlOldDoc)
        {
            StringBuilder content = new StringBuilder();
            if (!xmlOldDoc.HasChildNodes)
            {
                foreach (XmlNode node in xmlNewDoc.ChildNodes[0].ChildNodes)
                {
                    if (!node.Name.EndsWith("ID") && !excludedProp.Contains(node.Name) && !excludedProp.Contains(log.TableName + "_" + node.Name))
                        content.AppendLine(rm.GetString(log.TableName + "_" + node.Name) + ": " + node.InnerText);
                }
                return content;
            }

            int nodeCount = xmlNewDoc.ChildNodes[0].ChildNodes.Count;
            for (int i = 0; i < nodeCount; i++)
            {
                XmlNode node = xmlNewDoc.ChildNodes[0].ChildNodes[i];
                if (!node.Name.EndsWith("ID") && !excludedProp.Contains(node.Name) && !excludedProp.Contains(log.TableName + "_" + node.Name))
                {
                    XmlNode old = xmlOldDoc.ChildNodes[0].SelectSingleNode(node.Name);
                    if (old == null)
                        content.AppendLine(rm.GetString(log.TableName + "_" + node.Name) + ": " + node.InnerText);
                    else
                    {
                        if (old.InnerText != node.InnerText)
                            content.AppendLine(rm.GetString(log.TableName + "_" + node.Name) + ": " + old.InnerText + " => " + node.InnerText);

                        xmlNewDoc.ChildNodes[0].RemoveChild(node);
                        xmlOldDoc.ChildNodes[0].RemoveChild(old);
                        i--;
                        nodeCount--;
                    }
                }
            }

            if (xmlOldDoc.HasChildNodes)
            {
                foreach (XmlNode node in xmlOldDoc.ChildNodes[0].ChildNodes)
                {
                    if (!node.Name.EndsWith("ID") && !excludedProp.Contains(node.Name) && !excludedProp.Contains(log.TableName + "_" + node.Name))
                        content.AppendLine(rm.GetString(log.TableName + "_" + node.Name) + ": " + node.InnerText + " => xóa");
                }
            }

            return content;
        }

        private StringBuilder ShowUpdateZoneDetailLog(XmlDocument xmlNewDoc, XmlDocument xmlOldDoc)
        {
            string name = rm.GetString("ZoneDetails_Zone");

            StringBuilder content = new StringBuilder();
            if (!xmlOldDoc.HasChildNodes)
            {
                foreach (XmlNode node in xmlNewDoc.ChildNodes[0].ChildNodes)
                {
                    if (node.Name == "Zone")
                        content.AppendLine(name + ": " + node.InnerText);
                }
                return content;
            }

            int nodeCount = xmlNewDoc.ChildNodes[0].ChildNodes.Count;
            for (int i = 0; i < nodeCount; i++)
            {
                XmlNode node = xmlNewDoc.ChildNodes[0].ChildNodes[i];
                if (node.Name == "Zone")
                {
                    string zone = node.InnerText.Split(' ')[0];
                    XmlNodeList oldNodeList = xmlOldDoc.ChildNodes[0].SelectNodes("Zone");
                    XmlNode old = null;
                    foreach (XmlNode item in oldNodeList)
                    {
                        if (item.InnerText.StartsWith(zone))
                        {
                            old = item;
                            break;
                        }
                    }

                    if (old == null)
                        content.AppendLine(name + ": " + node.InnerText);
                    else
                    {
                        if (old.InnerText != node.InnerText)
                            content.AppendLine(name + ": " + old.InnerText + " => " + node.InnerText);

                        xmlOldDoc.ChildNodes[0].RemoveChild(old);
                        xmlNewDoc.ChildNodes[0].RemoveChild(node);
                        i--;
                        nodeCount--;
                    }
                }
            }

            if (xmlOldDoc.HasChildNodes)
            {
                foreach (XmlNode node in xmlOldDoc.ChildNodes[0].ChildNodes)
                {
                    if (node.Name == "Zone")
                        content.AppendLine(name + ": " + node.InnerText + " => xóa");
                }
            }

            return content;
        }

        #endregion

        #region Constructors

        public OrderLogDetailView()
        {
            InitializeComponent();

            _presenter = new BasePresenter(this);
        }

        #endregion

        #region Public Properties

        public override BaseENT Entity
        {
            set
            {
                LogENT log = value == null ? new LogENT() : value as LogENT;
                txtDate.Text = Utility.DateTimeParse(log.Date).ToString("dd/MM/yyyy hh:mm");
                txtAction.Text = log.Action;
                txtFullName.Text = log.FullName;
                txtTableName.Text = log.TableDescription;
                txtDescription.Text = log.Description;

                StringBuilder content = new StringBuilder();
                if (log.Action == "Thêm")
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    if (log.NewContent != null)
                    {
                        xmlDoc.LoadXml(log.NewContent);
                        foreach (XmlNode node in xmlDoc.ChildNodes[0].ChildNodes)
                        {
                            if (!node.Name.EndsWith("ID") && !excludedProp.Contains(node.Name) && !excludedProp.Contains(log.TableName + "_" + node.Name))
                                content.AppendLine(rm.GetString(log.TableName + "_" + node.Name) + ": " + node.InnerText);
                        }
                    }
                }
                else if (log.Action == "Sửa")
                {
                    XmlDocument xmlOldDoc = new XmlDocument();
                    XmlDocument xmlNewDoc = new XmlDocument();

                    if (log.OldContent != null)
                        xmlOldDoc.LoadXml(log.OldContent);
                    if (log.NewContent != null)
                        xmlNewDoc.LoadXml(log.NewContent);

                    if (xmlNewDoc.ChildNodes[0].Name == "ZoneDetails")
                        content = ShowUpdateZoneDetailLog(xmlNewDoc, xmlOldDoc);
                    else
                        content = ShowUpdateLog(log, xmlNewDoc, xmlOldDoc);
                }
                else if (log.Action == "Xóa")
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(log.OldContent);

                    foreach (XmlNode node in xmlDoc.ChildNodes[0].ChildNodes)
                    {
                        if (!node.Name.EndsWith("ID") && !excludedProp.Contains(node.Name) && !excludedProp.Contains(log.TableName + "_" + node.Name))
                            content.AppendLine(rm.GetString(log.TableName + "_" + node.Name) + ": " + node.InnerText);
                    }
                }

                rtbContent.Text = content.ToString();
            }
        }

        #endregion Public Properties
    }
}
